<?php

namespace Home\Controller;

use Think\Controller;
use Home\Lib\ModuleInfo;
use Home\Lib\DataEntry;
use Home\Lib\Discount;

class ModuleController extends Controller
{
    // 模块管理首页
    public function index()
    {
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->display();
    }

    // 基础信息页面
    public function baseInfo()
    {
        $baseInfo = M('base_info');
        $logdata = M('logdata');
        $baseData = array();
        if (I('basetype')) {
            $count = $baseInfo->count();
            $fpage = new \Think\Page($count, 10);
            $fpage->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;
              第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
            $fpage->setConfig('first', '首页');
            $fpage->setConfig('prev', '上一页');
            $fpage->setConfig('next', '下一页');
            $fpage->setConfig('last', '末页');
            $fpage->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
            $fpage->rollPage = 5;
            $fpage->lastSuffix = false;
            $show = $fpage->show();
            $baseData = $baseInfo->limit($fpage->firstRow.','.$fpage->listRows)->order('id')->select();
            foreach ($baseData as &$value) {
                $map = array('type' => I('basetype'),'uid' => session(C('USER_AUTH_KEY')),'batch' => $value['batch'],'project_code' => $value['project_code']);
                if ($resultLog = $logdata->where($map)->find()) {
                    $value['disable'] = true;
                    $value['uid'] = $resultLog['uid'];
                    $value['workload'] = $resultLog['workload'];
                    $value['finish'] = $resultLog['finish'];
                } else {
                    $value['disable'] = false;
                }
            }
        }
        $this->assign('show', $show);
        $this->assign('count', $count);
        $this->assign('listRows', $fpage->listRows);
        $this->assign('basetype', I('get.basetype'));
        $this->assign('baseData', $baseData);
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->display('baseInfo');
    }

    //录入数据页面
    public function logData()
    {
        if (IS_POST) {
            session('batch', I('post.batch'));
            session('project_code', I('get.project_code'));
            $this->assign('form', session('form'));
            $this->assign('username', session('user_name'));
            $this->display();
        } else {
            $this->redirect('Home/Module/baseInfo');
        }
    }

    //ajax请求添加模块
    public function choseModule()
    {
        if (IS_POST) {
            $module = M('module');
            if (I('post.status') == 'name') {
                $data = $module->where(array('number' => I('post.number')))->select();
                if (is_array($data)) {
                    echo json_encode(array('code' => 'ok', 'msg' => '成功', 'data' => $data));
                } else {
                    echo json_encode(array('code' => 'fail', 'msg' => '失败', 'data' => $data));
                }
            } elseif (I('post.status') == 'class') {
                $data = $module->where(array('number' => I('post.number'), 'type' => I('post.type')))->select();
                if (is_array($data)) {
                    echo json_encode(array('code' => 'ok', 'msg' => '成功', 'data' => $data));
                } else {
                    echo json_encode(array('code' => 'fail', 'msg' => '失败', 'data' => $data));
                }
            } elseif (I('post.status') == 'price') {
                $data = $module->where(array('number' => I('post.number'), 'type' => I('post.type'), 'class' => I('post.class'), 'module_name' => array('like', I('post.name'))))->find();
                if (is_array($data)) {
                    echo json_encode(array('code' => 'ok', 'msg' => '成功', 'data' => $data));
                } else {
                    echo json_encode(array('code' => 'fail', 'msg' => '失败', 'data' => $data));
                }
            } elseif (I('post.status') == 'discount') {
                $discount = M('discount');
                $map = array(
                    'class' => I('post.class'),
                    'mid' => I('post.mid'),
                    'type' => I('post.type'),
                    'lsection' => array('elt',I('post.buynumber')),
                    'rsection' => array('gt',I('post.buynumber')),
                    'uid' => session(C('USER_AUTH_KEY')),
                );
                $data = $discount->where($map)->find();
                if (is_array($data)) {
                    echo json_encode(array('code' => 'ok', 'msg' => '成功', 'data' => $data));
                } else {
                    $data = array('discount' => '0%');
                    echo json_encode(array('code' => 'fail', 'msg' => '失败', 'data' => $data));
                }
            } elseif (I('post.status') == 'add') {
                $bought = M('bought');
                $basenum = I('post.basenum') ? I('post.basenum') : 0;
                $arr = array(
                    'module_name' => I('post.name'),
                    'class' => I('post.class'),
                    'number' => I('post.number'),
                    'singleprice' => I('post.baseprice'),
                    'safeprice' => I('post.safeprice'),
                    'discount' => I('post.discount'),
                    'buynumber' => I('post.buynumber'),
                    'basenum' => $basenum,
                    'type' => I('post.type'),
                    'work_content' => I('post.remark'),
                    'uid' => session(C('USER_AUTH_KEY')),
                    'batch' => session('batch'),
                    'project_code' => I('post.project_code'),
                );
                $_SESSION['module'][] = $arr;//保存已选择的模块到session中
                if ($_SESSION['module']) {
                    echo json_encode(array('code' => 'ok', 'msg' => '该模块添加成功', 'data' => ''));
                } else {
                    echo json_encode(array('code' => 'ok', 'msg' => '没有模块被添加', 'data' => ''));
                }
            } elseif (I('post.status') == 'save') {
                $bought = M('bought');
                $logData = M('logdata');
                if ($logData->where(['project_code' => I('post.project_code'), 'batch' => session('batch'), 'uid' => session(C('USER_AUTH_KEY')), 'type' => I('post.type')])->find()) {
                    echo json_encode(array('code' => 'fail', 'msg' => '已经申报过', 'data' => '' ));
                } else {
                    if (session('module')) {
                        if ($bought->addAll(session('module'))) {
                            $map['uid'] = session(C('USER_AUTH_KEY'));
                            $map['batch'] = session('batch');
                            $map['project_code'] = I('post.project_code');
                            $map['type'] = I('post.type');
                            $data = $bought->where($map)->select();
                            session('module', null);
                            echo json_encode(array('code' => 'ok', 'msg' => '成功', 'data' => $data));
                        } else {
                            echo json_encode(array('code' => 'fail', 'msg' => '添加失败', 'data' => ''));
                        }
                    } else {
                        echo json_encode(array('code' => 'fail', 'msg' => '请选择模块', 'data' => ''));
                    }
                }
            } elseif (I('post.status') == 'look') {
                echo json_encode(array('code' => 'ok', 'msg' => 'yes', 'data' => session('module')));
            } elseif (I('post.status') == 'empty') {
                session('module', null);
                echo json_encode(array('code' => 'ok', 'msg' => '清空', 'data' => ''));
            }
        }
    }

    //信息录入
    public function DataEntry($type = '')
    {
        session('batch', I('batch'));
        $entry = new  DataEntry();
        if ($type == '土建专业') {
            $entry->tujian();
        } elseif ($type == '铁塔专业') {
            $entry->tieta();
        } elseif ($type == '交流专业') {
            $entry->jiaoliu();
        } elseif ($type == '配套专业') {
            $entry->peitao();
        } elseif ($type == 'submit') {
            $entry->submit();
        } else {
            $this->redirect('Home/Module/logData');
        }
    }

    // 模块信息
    public function moduleInfo($status = '')
    {
        $module = new ModuleInfo();
        if ($status == 'add') {
            $module->addModule();
        } elseif ($status == 'mod') {
            $module->modModule();
        } elseif ($status == 'del') {
            $module->delModule();
        } elseif ($status == 'addhandler') {
            $module->addModuleHandler();
        } elseif ($status == 'modhandler') {
            $module->modModuleHandler();
        } else {
            $module->getModuleInfo();
        }
    }

    // 优惠信息
    public function discount($status = '', $mid = '', $class = '')
    {
        $discount = new Discount();
        if ($status == 'info') {
            $discount->info($mid, $class);
        } elseif ($status == 'add') {
            $discount->add();
        } elseif ($status == 'addHandler') {
            $discount->addHandler();
        } elseif ($status == 'mod') {
            $discount->mod();
        } elseif ($status == 'modHandler') {
            $discount->modHandler();
        } elseif ($status == 'del') {
            $discount->del();
        }
    }
    // 数据核对
    public function verdata()
    {
        $verdata = D('VerdataView');
        $map = array(
        'uid' => session(C('USER_AUTH_KEY')),
        );
        $count = $verdata->where($map)->count();
        $fpage = new \Think\Page($count, 10);
        $fpage->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;
              第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $fpage->setConfig('first', '首页');
        $fpage->setConfig('prev', '上一页');
        $fpage->setConfig('next', '下一页');
        $fpage->setConfig('last', '末页');
        $fpage->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
        $fpage->rollPage = 5;
        $fpage->lastSuffix = false;
        $show = $fpage->show();
        $redata = $verdata->limit($fpage->firstRow.','.$fpage->listRows)->where($map)->order('id desc')->select();
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->assign('count', $count);
        $this->assign('verdata', $redata);
        $this->assign('listRows', $fpage->listRows);
        $this->assign('show', $show);
        $this->display();
    }
}
